package com.hownoon.parquet;


import com.hownoon.HN_IoFile;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecMyThread extends Thread  {
    Configuration configuration;
    Connection connection;
    String path;

    public ExecMyThread(String path) {
        this.path=path;
        this.start();
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        exec();
        long endTime = System.currentTimeMillis();
        System.out.println("所有程序总耗时《==》"+(endTime - startTime) + "ms");
    }

    public void exec(){


        System.out.println("******************************************************************************");
        System.out.println("即将开始插入数据");
        System.out.println("******************************************************************************");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            execMakeData(path);
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("******************************************************************************");
        System.out.println("全部数据已插入完毕");
        System.out.println("******************************************************************************");


    }
    /**
     * 制造数据线程池
     * @param
     * @author HowNoon
     * @time 2021/11/2 18:49
     */
    public void execMakeData(String path) throws IOException {
        //创建一个可重用固定20个数的线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","172.16.50.51:2181,172.16.50.52:2181,172.16.50.53:2181,172.16.50.54:2181,172.16.50.55:2181");

        connection= ConnectionFactory.createConnection(configuration);
        ArrayList<String> arrayList=HN_IoFile.getAllFileName(path);
        for(int i=0;i<arrayList.size();i++){
            ReadDataThread readDataThread =new ReadDataThread(connection,path+"/"+arrayList.get(i),String.valueOf(i+1));
            fixedThreadPool.execute(readDataThread);
        }
        fixedThreadPool.shutdown();

        while (true){
            if(fixedThreadPool.isTerminated()){
                try {
                    if(connection!=null){
                        connection.close();
                        System.out.println("connection连接已关闭");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

                System.out.println("******************************************************************************");
                System.out.println("制造数据线程池已关闭");
                System.out.println("******************************************************************************");
                break;
            }
        }

    }


}
